<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="历史版本" prop="hisVersion">
        <el-select @change="changeHisVersion($event)" v-model="queryParams.hisVersion" placeholder="请选择历史版本号" clearable>
          <el-option
            v-for="dict in hisVersions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属模块" prop="strModular">
        <treeselect style="width: 300px;" v-model="queryParams.strModular" :show-count="true"
                    :multiple="true"
                    :disable-branch-nodes="true"
                    @keyup.enter.native="handleQuery"
                    :options="modularTree"  placeholder="请选择所属模块" />
      </el-form-item>
      <el-form-item label="所属报表" prop="strReport">
        <treeselect style="width: 300px;" v-model="queryParams.strReport" :show-count="true"
                    :multiple="true"
                    :disable-branch-nodes="true"
                    @keyup.enter.native="handleQuery"
                    :options="reportTree"  placeholder="请选择所属报表" />
      </el-form-item>
      <el-form-item label="所属客户" prop="strCustomer">
        <treeselect style="width: 300px;" v-model="queryParams.strCustomer" :show-count="true"
                    :multiple="true"
                    :disable-branch-nodes="true"
                    @keyup.enter.native="handleQuery"
                    :options="customerTree"  placeholder="请选择所属客户" />
      </el-form-item>
      <el-form-item label="表名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="chineseName">
        <el-input
          v-model="queryParams.chineseName"
          placeholder="请输入中文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在分层" prop="layered" v-show="showSearchColl">
        <el-select v-model="queryParams.layered" placeholder="请选择所在分层" clearable>
          <el-option
            v-for="dict in dict.type.report_table_layered"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="ftype" v-show="showSearchColl">
        <el-select v-model="queryParams.ftype" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.report_table_ftype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="fsource" v-show="showSearchColl">
        <el-select v-model="queryParams.fsource" placeholder="请选择来源" clearable>
          <el-option
            v-for="dict in dict.type.report_table_fsource"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="版本号" prop="version" v-show="showSearchColl">
        <el-select v-model="queryParams.version" placeholder="请选择版本号" clearable>
          <el-option
            v-for="dict in dict.type.report_version"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status" v-show="showSearchColl">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:table:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reportworld:table:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:table:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCopy"
          v-hasPermi="['reportworld:table:copy']"
        >复制</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:table:export']"
        >导出</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="tableCreateInfo"
          v-hasPermi="['reportworld:table:create']"
        >导出建表语句</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateBatch"
          v-hasPermi="['reportworld:table:batchEdit']"
        >批量修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="readTableInfo"
          v-hasPermi="['reportworld:table:readTableInfo']"
        >获取表信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="readDemandDesign"
          v-hasPermi="['reportworld:table:readDemandDesign']"
        >读取设计文档</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="getDatamapInfoBySql"
          v-hasPermi="['reportworld:datamap:get']"
        >获取依赖信息</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" :showSearchColl.sync="showSearchColl" :searchColl="searchColl" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" v-loading="loading" max-height="550px"
              :data="tableList" @selection-change="handleSelectionChange"
              @sort-change="handleSortChange"
              :row-style="tableRowStyle"  @row-click="rowClick" ref="table" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-hasPermi="['reportworld:table:query','reportworld:table:list',
      'reportworld:indexed:list','reportworld:table:demandBug','reportworld:table:data:comparedata',
      'reportworld:table:datamap:img']"
        label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row,false)"
            v-hasPermi="['reportworld:table:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTableDetail(scope.row)"
            v-hasPermi="['reportworld:table:list']"
          >字段信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="tableIndexed(scope.row)"
            v-hasPermi="['reportworld:indexed:list']"
          >索引管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="tableDemandBug(scope.row)"
            v-hasPermi="['reportworld:table:demandBug']"
          >修改记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="comparedata(scope.row)"
            v-hasPermi="['reportworld:table:data:comparedata']"
          >历史记录</el-button>
          <el-button v-if="scope.row.isDataMap == 1"
            type="text"
            icon="el-icon-setting"
            size="mini"
            @click="datamapimg(scope.row)"
            v-hasPermi="['reportworld:datamap:img']"
          >数据地图</el-button>
      <!--    <el-button
            v-if="scope.row.layered != 2"
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="dataMapRelyInfo(scope.row)"
            v-hasPermi="['reportworld:datamap:list']"
          >表依赖信息</el-button>-->
          <!--<el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['reportworld:indexed:list', 'reportworld:table:demandBug',
          'reportworld:table:data:comparedata','reportworld:datamap:img']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item  command="tableIndexed" icon="el-icon-check"
                                v-hasPermi="['reportworld:indexed:list']">索引管理</el-dropdown-item>
              <el-dropdown-item  command="tableDemandBug" icon="el-icon-edit"
                                v-hasPermi="['reportworld:table:demandBug']">修改记录</el-dropdown-item>
              <el-dropdown-item  command="comparedata" icon="el-icon-plus"
                                 v-hasPermi="['reportworld:table:data:comparedata']">历史记录</el-dropdown-item>
              <el-dropdown-item  command="datamapimg" icon="el-icon-download"
                                 v-hasPermi="['reportworld:datamap:img']">流向图</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>-->
        </template>
      </el-table-column>
      <el-table-column label="表名" align="center" prop="name" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="中文名称" align="center" prop="chineseName" width="150" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="所在分层" width="150" align="center" prop="layered" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_table_layered" :value="scope.row.layered"/>
        </template>
      </el-table-column>
      <el-table-column label="所属模块"  width="150" align="center" prop="strModularName" :show-overflow-tooltip="true" />
      <el-table-column label="所属报表"  width="150" align="center" prop="strReportName" :show-overflow-tooltip="true" />
      <el-table-column label="所属客户"  width="150" align="center" prop="strCustomerName" :show-overflow-tooltip="true" />
      <el-table-column label="版本号" width="150" align="center" prop="version" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_version" :value="scope.row.version" sortable="custom" :sort-orders="['descending', 'ascending']"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="ftype" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_table_ftype" :value="scope.row.ftype"/>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="fsource" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_table_fsource" :value="scope.row.fsource"/>
        </template>
      </el-table-column>
      <el-table-column label="原始表" width="150" align="center" prop="originalTableName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报标世界信息对话框 -->
    <el-dialog  :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="表名" maxlength="100" v-if="batchEdit== false" prop="name">
              <el-input clearable v-model="form.name" placeholder="请输入表名"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="中文名称" maxlength="200" v-if="batchEdit == false" prop="chineseName">
              <el-input clearable v-model="form.chineseName" placeholder="请输入中文名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在分层" prop="layered">
              <el-select clearable v-model="form.layered" placeholder="请选择所在分层">
                <el-option
                  v-for="dict in dict.type.report_table_layered"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属模块" prop="strModular">
              <treeselect  v-model="form.strModular"
                          :show-count="true"
                          :multiple="true"
                          :disable-branch-nodes="true"
                          :options="modularTree"
                           placeholder="请选择所属模块"/>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== true" :span="12">
            <el-form-item label="清空所属模块" prop="clearModular">
              <el-switch v-model="form.clearModular"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属客户" prop="strCustomer">
              <treeselect  v-model="form.strCustomer"
                          :show-count="true"
                          :multiple="true"
                          :disable-branch-nodes="true"
                          :options="customerTree"
                           placeholder="请选择所属客户"/>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== true" :span="12">
            <el-form-item label="清空所属客户" prop="clearCustomer">
              <el-switch v-model="form.clearCustomer"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属报表" prop="strReport">
              <treeselect  v-model="form.strReport"
                          :show-count="true"
                          :multiple="true"
                          :disable-branch-nodes="true"
                          :options="reportTree"
                           placeholder="请选择所属报表"/>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== true" :span="12">
            <el-form-item label="清空所属报表" prop="clearReport">
              <el-switch v-model="form.clearReport"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="原始表" prop="originalTableName">
              <el-select style="width: 100%;" filterable clearable v-model="form.originalTableName"
                         placeholder="请选择原始表">
                <el-option
                  v-for="item in tables"
                  :key="item.id"
                  :label="`${item.name}【${item.chineseName}】`"
                  :value="item.name"
                  :disabled="parseInt(item.status) == 2"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== false" :span="12">
            <el-form-item label="版本号" prop="version">
              <el-select :disabled="true" clearable v-model="form.version" placeholder="请选择版本号">
                <el-option
                  v-for="dict in dict.type.report_version"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== true" :span="12">
            <el-form-item label="状态" prop="status">
              <el-select clearable v-model="form.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="batchEdit== false"  :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="ftype">
              <el-select clearable v-model="form.ftype" placeholder="请选择类型">
                <el-option
                  v-for="dict in dict.type.report_table_ftype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源" prop="fsource">
              <el-select clearable v-model="form.fsource" placeholder="请选择来源">
                <el-option
                  v-for="dict in dict.type.report_table_fsource"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input maxlength="500" clearable v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 导出选择数据库类型 -->
    <el-dialog v-if="openDatabaseType"  :title="titleDatabaseType"  :close-on-click-modal ="false" :visible.sync="openDatabaseType" width="500px" height="100px" append-to-body>
      <el-form ref="formDatabaseType" :model="formDatabaseType" :rules="rulesDatabaseType"  label-width="100px">
        <el-row>
          <el-col :span="24" style="height:100px ">
            <el-form-item label="数据库类型" prop="dataBaseType">
              <el-select clearable v-model="formDatabaseType.dataBaseType"
                         placeholder="请选择数据库类型">
                <el-option
                  v-for="dict in dict.type.report_database_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTableCreateInfo">确 定</el-button>
        <el-button @click="openDatabaseType = false">关 闭</el-button>
      </div>
    </el-dialog>


    <!-- 读取设计文档-->
    <el-dialog v-if="upload.open" :title="upload.title" :close-on-click-modal ="false" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+'&hisVersion='+ this.queryParams.hisVersion"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否基础页面文档
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
         <!-- <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>-->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- SQL检查和执行结果 -->
    <el-dialog v-if="openMsg" :title="titleMsg"  :visible.sync="openMsg" :close-on-click-modal ="false" width="1000px" height="500px" append-to-body>
      <el-col :span="24" style="height:500px ">
        <textarea  v-model="msg" style="width:100%;height: 492px" ></textarea>
      </el-col>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openMsg = false">关 闭</el-button>
      </div>
    </el-dialog>


    <el-dialog v-if="openComparedata"  :title="titleComparedata" :close-on-click-modal ="false"  :visible.sync="openComparedata" width="1200px" append-to-body>
      <compare :dataId="dataId" :dataCode="dataCode"> </compare>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openComparedata = false">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog v-if="openDatademandbug"  :title="titleDatademandbug" :close-on-click-modal ="false"  :visible.sync="openDatademandbug" width="1200px" append-to-body>
      <datademandbug :dataId="dataId" :dataCode="dataCode" :hisVersion="hisVersion"> </datademandbug>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDatademandbug = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listTable, getTable, delTable, addTable, updateTable,init,handleUpdateBatch ,readTableInfo} from "@/api/reportworld/table";
  import { getDatamapInfoBySql } from "@/api/reportworld/datamap";
  import { getToken } from "@/utils/auth";
  import { getTableRowStyle } from "@/utils/yss";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import compare from "@/views/reportworld/comparedata/index";
  import datademandbug from "@/views/reportworld/demandbuginfo/datademandbug";

  export default {
    name: "Table",
    components: {
      Treeselect,
      compare,
      datademandbug
    },
    dicts: ['report_table_ftype', 'sys_common_status', 'report_version', 'report_table_layered', 'report_table_fsource','report_database_type'],
    data() {
      return {
        butloading:false,
        isUpdate:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        searchColl:true,
        showSearchColl:false,
        // 总条数
        total: 0,
        // 报标世界信息表格数据
        tableList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //所属模块
        modularTree : [],
        // 所属客户
        customerTree: [],
        //所属报表
        reportTree: [],
        //表信息
        tables: [],
        // 历史版本号
        hisVersions:[],
        hisVersion:'',
        // 批量修改需要隐藏的字段
        batchEdit: false,
        signType :'',
        // 导出语句弹出
        titleDatabaseType: "",
        openDatabaseType:false,
        //历史数据
        titleComparedata:'',
        openComparedata:false,
        titleDatademandbug:'',
        openDatademandbug:false,
        dataId:'',
        dataCode:'',
        openMsg:false,
        titleMsg:"",
        msg:"",
        // 导入参数
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/reportworld/table/readDemandDesign"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 50,
          hisVersion:null,
          name: null,
          chineseName: null,
          layered: null,
          version: null,
          ftype: null,
          fsource: null,
          status: null,
          strModular:[],
          strCustomer:[],
          strReport:[]
        },
        // 表单参数
        form: {},
        formDatabaseType:{},
        // 表单校验
        rules: {
        },
        rulesDatabaseType:{
          dataBaseType:[{ required: true, message: '请选择数据库类型',trigger: 'blur'}]
        }
      };
    },
    created() {
      this.init();
      this.getList();
    },
    methods: {
      changeHisVersion(value){
        this.queryParams.hisVersion=value;
        this.init();
        this.getList();
      },
      /** 查询表信息列表 */
      getList() {
        this.loading = true;
        listTable(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
      });
      },
      init(){
         /* this.$set(this.queryParams, "strModular", null);
          this.$set(this.queryParams, "strCustomer", null);
          this.$set(this.queryParams, "strReport", null);*/
        init(this.queryParams).then(response => {
          this.modularTree=response.modularTree;
          this.customerTree=response.customerTree;
          this.reportTree=response.reportTree;
          this.tables=response.tables;
          this.hisVersions=response.hisVersions;
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.openDatabaseType =false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          chineseName: null,
          description: null,
          layered: null,
          version: null,
          ftype: null,
          fsource: null,
          originalTableName: null,
          status: null,
          fileName: null,
          createTime: null,
          createBy: null,
          updateTime: null,
          updateBy: null,
          remark: null,
          strModular:[],
          strCustomer:[],
          strReport:[],
          hisVersion:null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      tableIndexed(row){
        const id = row.id;
        const tableCode= row.name;
        //this.$router.push("/reportworld/table-indexed/index/" + id);
        this.$router.push({ path:"/reportworld/table-indexed/index/" + id+this.queryParams.hisVersion,
          query: {tableId:id,hisVersion:this.queryParams.hisVersion} });
      },
      tableDemandBug(row){
        this.openDatademandbug =true;
        this.dataId = row.id;
        this.dataCode = 'REPORT_TABLE_CODE';
        this.hisVersion=this.queryParams.hisVersion;
        this.titleDatademandbug = "表结构修改记录信息";
      },
      comparedata(row){
        this.openComparedata =true;
        this.dataId = row.id;
        this.dataCode = 'REPORT_TABLE_CODE';
        this.titleComparedata = "表结构历史记录信息";
      },
      datamapimg(row){
        const id = row.id;
        const tableName= row.name;
        //this.$router.push("/reportworld/data-map/tableName/" + tableCode);
        // this.$router.push("/reportworld/data-map-img/tableName/" + tableName);
        this.$router.push({ path:"/reportworld/data-map-img/tableName/" + tableName+this.queryParams.hisVersion,
          query: {tableName:tableName,dataCode:'REPORT_TABLE_CODE',hisVersion:this.queryParams.hisVersion} });
      },
      dataMapRelyInfo(row){
        const tableName= row.name;
        //this.$router.push("/reportworld/data-map/tableName/" + tableName);
        this.$router.push({ path:"/reportworld/data-map/tableName/" + tableName+this.queryParams.hisVersion,
          query: {tableName:tableName,hisVersion:this.queryParams.hisVersion} });
      },
      // 更多操作触发
     /* handleCommand(command, row) {
        const id = row.id;
        const tableCode= row.name;
        switch (command) {
          case "tableIndexed":
            this.$router.push("/reportworld/table-indexed/index/" + id);
            break;
          case "tableDemandBug":
            this.$router.push({ name:'dataDemandBug',query: {dataId:id,dataCode:'REPORT_TABLE_CODE'} });
            break;
          case "comparedata":
            this.handleComparedata(row.id,'REPORT_TABLE_CODE');
            break;
          case "datamapimg":
            this.$router.push("/reportworld/data-map/tableName/" + tableCode);
            break;
          default:
            break;
        }
      },*/
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.rules = {
          name: [
            {required: true, message: "表名不能为空", trigger: "blur" }
          ],
          chineseName: [
            { required: true, message: "中文名称不能为空", trigger: "blur" }
          ],
          layered: [
            { required: true, message: "所在分层不能为空", trigger: "change" }
          ],
          version: [
            { required: true, message: "版本号不能为空", trigger: "change" }
          ],
          ftype: [
            { required: true, message: "类型不能为空", trigger: "change" }
          ],
          fsource: [
            { required: true, message: "来源不能为空", trigger: "change" }
          ],
          status: [
            { required: true, message: "状态不能为空", trigger: "change" }
          ],
        };
        this.open = true;
        this.title = "添加表信息";
        this.isUpdate =true;
        this.batchEdit=false;
        this.form.layered = parseInt(this.dict.type.report_table_layered[0].value);
        this.form.version = this.dict.type.report_version[0].value;
        if(this.queryParams.hisVersion){
          this.form.version =this.queryParams.hisVersion;
        }
        this.form.ftype = parseInt(this.dict.type.report_table_ftype[0].value);
        this.form.fsource = parseInt(this.dict.type.report_table_fsource[0].value);
        this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
      },
      /** 复制操作 */
      handleCopy(row) {
        this.handleAdd();
        const id = row.id || this.ids
        this.queryParams.id=id[0];
        getTable(this.queryParams).then(response => {
          this.form = response.data;
          this.$set(this.form, "strModular", response.data.strModular);
          this.$set(this.form, "strCustomer", response.data.strCustomer);
          this.$set(this.form, "strReport", response.data.strReport);
          this.form.id = null;
          this.form.version = this.dict.type.report_version[0].value;
          if(this.queryParams.hisVersion){
            this.form.version =this.queryParams.hisVersion;
          }
      });
      },
      /** 修改按钮操作 */
      handleUpdate(row,isUpdate) {
        this.reset();
        const id = row.id || this.ids
        this.rules = {
          name: [
            { required: true, message: "表名不能为空", trigger: "blur" }
          ],
          chineseName: [
            { required: true, message: "中文名称不能为空", trigger: "blur" }
          ],
          layered: [
            { required: true, message: "所在分层不能为空", trigger: "change" }
          ],
          version: [
            { required: true, message: "版本号不能为空", trigger: "change" }
          ],
          ftype: [
            { required: true, message: "类型不能为空", trigger: "change" }
          ],
          fsource: [
            { required: true, message: "来源不能为空", trigger: "change" }
          ],
          status: [
            { required: true, message: "状态不能为空", trigger: "change" }
          ],
        };
        if(isUpdate == false){
          this.queryParams.id=id;
        }else{
          this.queryParams.id=id[0];
        }
        getTable(this.queryParams).then(response => {
          this.form = response.data;
          this.$set(this.form, "strModular", response.data.strModular);
          this.$set(this.form, "strCustomer", response.data.strCustomer);
          this.$set(this.form, "strReport", response.data.strReport);
          this.open = true;
          this.title = "修改表信息";
          this.batchEdit=false;
          this.isUpdate =true;
        if(isUpdate == false){
          this.title = "表信息详情";
          this.isUpdate =false;
        }
      });
      },
      handleUpdateBatch(row) {
        this.reset();
        this.open = true;
        this.title = "批量修改表信息";
        this.rules = {};
        this.batchEdit=true;
        this.isUpdate =true;
      },
      readTableInfo(){
        if(!this.queryParams.name){
           return this.$modal.alertError("请输入要导入的表");
        }
        readTableInfo(this.queryParams).then(response => {
          this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
      },
      getDatamapInfoBySql(){
        const hisVersion=this.queryParams.hisVersion;
        this.$modal.loading("正在获取依赖信息中，请稍候...");
        getDatamapInfoBySql(hisVersion).then(response => {
         this.msg = response.msg;
         this.openMsg = true;
         this.titleMsg ="获取依赖信息结果";
         this.$modal.closeLoading()
      });
      },
      readDemandDesign(){
        this.upload.title = "读取设计文档";
        this.upload.open = true;
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.msg = response.msg;
        this.openMsg = true;
        this.titleMsg ="获取依赖信息结果";
        // this.$alert("<div style='overflow: auto;max-height: 70vh;max-width: 400vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      /** 表字段信息 */
      handleTableDetail: function(row) {
        const id = row.id;
      //  this.$router.push("/reportworld/table-detail/index/?tableId=" + id+"&hisVersion="+this.queryParams.hisVersion);
        this.$router.push({ path:"/reportworld/table-detail/index/" + id+this.queryParams.hisVersion
          ,query: {tableId:id,hisVersion: this.queryParams.hisVersion} });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.butloading=true;
            this.form.hisVersion =this.queryParams.hisVersion;
            if(this.batchEdit==true){
              this.form.idList= this.ids;
              handleUpdateBatch(this.form).then(response => {
              this.open = false;
              this.$modal.msgSuccess("修改成功");
              this.getList();
            }).finally(() => {this.butloading=false});
            } else if (this.form.id != null) {
              updateTable(this.form).then(response => {
              this.open = false;
              this.$modal.msgSuccess("修改成功");
              this.getList();
            }).finally(() => {this.butloading=false});
            } else {
              addTable(this.form).then(response => {
              this.open = false;
              this.$modal.msgSuccess("新增成功");
              this.getList();
            }).finally(() => {this.butloading=false});
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const hisVersion= this.queryParams.hisVersion;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delTable(ids,hisVersion);
        }).then(() => {
          this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.queryParams.ids=JSON.stringify(this.ids);
        this.download('reportworld/table/export', {
          ...this.queryParams
      }, `报表世界表结构信息_${new Date().getTime()}.xlsx`)
      },
      tableCreateInfo(){  // 导出选择数据库
        this.openDatabaseType=true;
        this.titleDatabaseType ="导出建表语句";
      },
      submitTableCreateInfo(){
        this.$refs["formDatabaseType"].validate(valid => {
          if (valid) {
            this.openDatabaseType=false;
            this.queryParams.DATABASETYPE =this.formDatabaseType.dataBaseType;
            this.queryParams.ids=JSON.stringify(this.ids);
            this.download('reportworld/table/tableCreateInfo', {
              ...{"queryJson":JSON.stringify(this.queryParams)}
          }, `${new Date().getTime()}.sql`)
          }
        });
      },
      tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
      },
      rowClick(row, column, event) { //单击行
        this.$refs.table.toggleRowSelection(row);
      }
    }
  };
</script>
